package cn.sytton.taffe.web.sys.controller;

import cn.sytton.taffe.web.sys.entity.RoleEntity;
import cn.sytton.taffe.web.sys.entity.RoleUserEntity;
import cn.sytton.taffe.web.sys.entity.UserEntity;
import cn.sytton.taffe.web.sys.service.IRoleMenuService;
import cn.sytton.taffe.web.sys.service.IRoleService;
import com.github.pagehelper.PageInfo;
import cn.sytton.taffe.excel.annotation.ExportExcel;
import cn.sytton.taffe.framework.base.controller.BaseController;
import cn.sytton.taffe.framework.base.entity.PageParam;
import cn.sytton.taffe.web.sys.entity.Auth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 角色控制层
 *
 * @author skyrock
 */
@Tag(name = "角色")
@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/role")
public class RoleController extends BaseController {

    private final IRoleService roleService;
    private final IRoleMenuService roleMenuService;

    @Operation(summary = "查询分页角色列表")
    @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    @ExportExcel
    @GetMapping
    public PageInfo<RoleEntity> findPage(PageParam pp) {
        return roleService.findListPageOfXml(pp);
    }

    @Operation(summary = "保存角色")
    @PostMapping
    public void save(@RequestBody @Validated RoleEntity entity) {
        roleService.createOrUpdate(entity);
    }

    @Operation(summary = "查询单个角色")
    @Parameter(name = "id", description = "角色ID", in = ParameterIn.PATH, required = true)
    @GetMapping("/{id}")
    public RoleEntity findById(@PathVariable String id) {
        return roleService.findById(id);
    }

    @Operation(summary = "查询多个角色", description = "body: 主键ID数组")
    @PostMapping("/ids")
    public List<RoleEntity> findByIds(@RequestBody String[] ids) {
        return roleService.findByIds(ids);
    }

    @Operation(summary = "删除角色", description = "body: 主键ID数组")
    @PostMapping("/delete")
    public void delete(@RequestBody String[] ids) {
        roleService.delete(ids);
    }

    @Operation(summary = "查询用户分页列表")
    @Parameters({
            @Parameter(name = "roleId", description = "角色ID", in = ParameterIn.PATH, required = true),
            @Parameter(name = "pp", description = "分页参数对象", in = ParameterIn.QUERY, required = true)
    })
    @GetMapping("/user/{roleId}")
    public PageInfo<UserEntity> findUserListPage(@PathVariable String roleId, PageParam pp) {
        return roleService.findUserListPage(roleId, pp);
    }

    @Operation(summary = "添加角色用户")
    @PostMapping("/user/add")
    public void addUsers(@RequestBody RoleUserEntity entity) {
        roleService.addUsers(entity);
    }

    @Operation(summary = "移除角色用户")
    @PostMapping("/user/remove")
    public void removeUsers(@RequestBody RoleUserEntity entity) {
        roleService.removeUsers(entity);
    }

    @Operation(summary = "修改角色状态")
    @PostMapping("/status/update")
    public void updateRoleStatus(@RequestBody RoleEntity entity) {
        roleService.updateRoleStatus(entity);
    }

    @Operation(summary = "导入角色", description = "导入请求样例,返回导入结果文件名")
    @PostMapping("/import")
    public String importData(@RequestBody MultipartFile file) {
        return roleService.importData(file);
    }

    @Operation(summary = "获取角色的授权菜单ID及功能ID")
    @Parameter(name = "roleId", description = "角色ID", in = ParameterIn.QUERY, required = true)
    @GetMapping("/menu")
    public Map<String, Object> findMenu(@RequestParam String roleId) {
        return roleMenuService.findMenu(roleId);
    }

    @Operation(summary = "授权菜单及权限")
    @PostMapping("/menu/auth")
    public void authMenu(@RequestBody Auth auth) {
        roleMenuService.authMenu(auth);
    }
}
